<?php
/**
 * Created by PhpStorm.
 * User: N
 * Date: 2019/5/30
 * Time: 13:28
 */

namespace app\index\controller;


use app\index\model\doGoods;
use app\index\model\doUser;
use think\Db;
use think\facade\Request;


class Classcontroller extends Base
{
    public function clist(){
        $clist = Db::name("keti")->order("id desc")->paginate(10);
        $this->assign("page",$clist->render());
        $this->assign("data",$clist);
        return $this->fetch();
    }
    public function addclass(){
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            $data['if_two'] = 0;
            unset($data['file']);
            $res = Db::name("keti")->insert($data);
            if($res){
                ajaxMsg("1",'添加成功');
            }
            ajaxMsg('0','网络错误');
        }
        return $this->fetch();
    }
    public function editclass(){
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            // $data['etime'] = strtotime($data['etime']);
            unset($data['file']);
            $res = Db::name("keti")->where("id",$data['id'])->update($data);
            
            if($res){
                ajaxMsg("1",'添加成功');
            }
            ajaxMsg('0','网络错误');
        }
        $id = input("id");
        $info = Db::name("keti")->where("id",$id)->find();
        $this->assign("info",$info);
        return $this->fetch();
    }
    public function changeclass(){
        
    }
    public function delclass(){
        $id = input("id");
        $res = Db::name("keti")->where("id",$id)->delete();
        if($res){
            ajaxMsg(1,"删除成功");
        }
        ajaxMsg(0,'网络错误,删除失败');
    }
    public function member(){
        $id = input("id");
        $info = Db::name("member_class")->where("cid",$id)->select();
        foreach($info as &$val){
           switch ($val['type']) {
                case '普通高校、高职院校':
                   // code...
                   $val['title'] = '高校审核';
                   break;
                case '其他中小学/中职/特教学校':
                   // code...
                   $val['title'] = '教育局审核';
                   break;
                case '直属校':
                   // code...
                   $val['title'] = '高校审核';
                   break;
               default:
                   // code...
                   $val['title'] = '';
                   break;
           }
        }
        $this->assign("info",$info);
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function addmember(){
        $id = input('cid');
        // echo $id;die;
        if(Request::ispost()){
            $data = input("post.");
            $data['status'] = 0;
            $data['time'] = time();
            $data['anum'] = 0;
         
            $res = Db::name("member_class")->insert($data);
            if($res){
                ajaxMsg('1','添加成功');
            }
            ajaxMsg("0","网络错误,添加失败");
        }
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function memneredit(){
        $id = input('id');
        if(Request::isPost()){
            $data = input("post.");
            $res = Db::name("member_class")->where("id",$data['id'])->update($data);
            if($res){
                ajaxMsg('1','更新成功');
            }
            ajaxMsg("0","网络错误,更新失败");
        }
        $info = Db::name("member_class")->where("id",$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function memberstatus(){
        $where['id'] = input('id');
        $switch = input('status');

        if ($switch == "true") $data['status'] = 1;
        else $data['status'] = 0;

        $res = upDate('member_class',$where,$data);
        if ($res)ajaxMsg(1,'更改成功');
        ajaxMsg(0,'网络不佳修改失败');
    }
    public function delmember(){
        $id = input("id");
        $res = Db::name("member_class")->where("id",$id)->delete();
        if($res){
            ajaxMsg('1','删除成功');
        }
        ajaxMsg("0","网络错误,删除失败");
    }
    
    public function uploadfield(){
        $file = request()->file('file');
        $res = Upload($file,[],'/uploads/field/');
//        filePath
        ajaxMsg(1,$res['filePath']);
    }
    public function dafen(){
        if($this->admin['school']){
            if($this->admin['z_type']=='全部课题'){
                $type = ['一般课题','重点课题'];
            }else{
                $type = [$this->admin['z_type']];
            }
            $list = Db::name("shenqing as sq")
                ->leftjoin('keti kt','kt.id=sq.cid')
                ->leftjoin("admin ad",'ad.id=sq.admin_id')
                // ->leftjoin('dafen df','cid')
                ->where("ad.school",$this->admin['school'])
                ->where("ad.area","in",[2,52])
                ->where('sq.status','not in','0,5')
                ->where('if_inportant','in',$type)
                ->where("ad.time>sq.time")
                ->order("sq.id desc")
                ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content")
                ->paginate(10);

            $this->assign('page',$list->render());
            //根据学校种类.生成审核部门
            $title = [];
            
            $cate = "普通高校、高职院校";
            
            if($cate=="普通高校、高职院校"){
                $title[0] = '学校审核';
                $title[1] = '中心审核';
                
            }else if($cate=="其他中小学/中职/特教学校"){
                $title[0] = '教委审核';
                $title[1] = '中心审核';
            }else if($cate=="直属校"){
                $title[0] = '中心审核';
                $title[1] = '学校审核';
            }
            // dump($title);die;
            $this->assign('title',$title);
            $list = $list->toArray();
            $new_ary = [];
        
            foreach($list['data'] as $key=>$val){
                $new_ary[$key] = $val;
                $new_ary[$key]['content'] = json_decode($val['content'],true);
                $res = Db::name("dafen")->where('sid',$val['id'])->where('admin_id',$this->admin['id'])->find();
                if($res){
                    $new_ary[$key]['fen'] = $res['fen'];
                    $new_ary[$key]['dfstatus'] = $res['status'];
                }else{
                    $new_ary[$key]['fen'] = 0;
                    $new_ary[$key]['dfstatus'] = 0;
                }
                
                foreach($title as $k=>$v){
           
                    $new_ary[$key]['statusdes'][$k] = getstatus($v,$val['status']);
                    if($k==0){
                        $new_ary[$key]['count'][$k] = $val['count1'];
                    }else{
                        $new_ary[$key]['count'][$k] = $val['count2'];
                    }
                }
            }
           
            if($cate!="普通高校、高职院校"&&$cate != "直属校"){
                $new_ary =[];
                $this->assign('page','');
            }
            $admin = Db::name('admin')->where('school',$this->admin['school'])->where('lv',3)->find();
            $this->assign('bz',$admin['bz']);
            $this->assign('data',$new_ary);
            return $this->fetch();
        }elseif($this->admin['area']){
            if($this->admin['z_type']=='全部课题'){
                $type = ['一般课题','重点课题'];
            }else{
                $type = [$this->admin['z_type']];
            }
            // dump($type);die;
             $list = Db::name("shenqing as sq")
                ->leftjoin('keti kt','kt.id=sq.cid')
                ->leftjoin("admin ad",'ad.id=sq.admin_id')
                ->where("ad.area",$this->admin['area'])
                ->where("sq.status",'not in',"0,1,5")
                ->where('if_inportant','in',$type)
                ->order("sq.id desc")
                ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content")
                ->paginate(10);
                
            $this->assign('page',$list->render());
            $title = [];
  
        //查询学校分类
 
            $title[0] = '教委审核';
            $title[1] = '中心审核';
      
        $this->assign('title',$title);
        $list = $list->toArray();
        $new_ary = [];
       
        foreach($list['data'] as $key=>$val){
            $new_ary[$key] = $val;
            $new_ary[$key]['content'] = json_decode($val['content'],true);
            $res = Db::name("dafen")
                                   
                                    ->where("admin_id",$this->admin['id'])
                                    ->where("sid",$val['id'])
                                    ->find();
            if($res){
                    $new_ary[$key]['fen'] = $res['fen'];
                    $new_ary[$key]['dfstatus'] = $res['status'];
            }else{
                    $new_ary[$key]['fen'] = 0;
                    $new_ary[$key]['dfstatus'] = 0;
            }
            foreach($title as $k=>$v){
                $new_ary[$key]['statusdes'][$k] = getstatus($v,$val['status']);
            }
        }
        //  dump($new_ary);die;
        $admin = Db::name('admin')->where('area',$this->admin['area'])->where('lv',2)->find();
        $this->assign('bz',$admin['bz']);
        $this->assign('data',$new_ary);
        return $this->fetch();
        }else{
            // echo 123;die;
            if($this->admin['z_type']=='全部课题'){
                $type = ['一般课题','重点课题'];
            }else{
                $type = [$this->admin['z_type']];
            }
            $list = Db::name("shenqing as sq")
                ->leftjoin('keti kt','kt.id=sq.cid')
                ->leftjoin("admin ad",'ad.id=sq.admin_id')
                ->order("sq.id desc")
                ->where('sq.if_inportant','in',$type)
                ->where('sq.status','not in','0,1,2,4,5')
                ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content,ad.school")
                ->paginate(10);

            $this->assign('page',$list->render());
            //根据学校种类.生成审核部门
            $title = [];
            
            $cate = "普通高校、高职院校";
            
            if($cate=="普通高校、高职院校"){
                $title[0] = '学校审核';
                $title[1] = '中心审核';
                
            }else if($cate=="其他中小学/中职/特教学校"){
                $title[0] = '教委审核';
                $title[1] = '中心审核';
            }else if($cate=="直属校"){
                $title[0] = '中心审核';
                $title[1] = '学校审核';
            }
            // dump($title);die;
            $this->assign('title',$title);
            $list = $list->toArray();
            $new_ary = [];
        
            foreach($list['data'] as $key=>$val){
                $new_ary[$key] = $val;
                $new_ary[$key]['content'] = json_decode($val['content'],true);
                $res = Db::name("dafen")->where('sid',$val['id'])->where('admin_id',$this->admin['id'])->find();
                if($res){
                    $new_ary[$key]['fen'] = $res['fen'];
                    $new_ary[$key]['dfstatus'] = $res['status'];
                }else{
                    $new_ary[$key]['fen'] = 0;
                    $new_ary[$key]['dfstatus'] = 0;
                }
                
                foreach($title as $k=>$v){
           
                    $new_ary[$key]['statusdes'][$k] = getstatus($v,$val['status']);
                    if($k==0){
                        $new_ary[$key]['count'][$k] = $val['count1'];
                    }else{
                        $new_ary[$key]['count'][$k] = $val['count2'];
                    }
                }
            }
            // dump($new_ary);die;
            if($cate!="普通高校、高职院校"&&$cate != "直属校"){
                $new_ary =[];
                $this->assign('page','');
            }
            $admin = Db::name('admin')->where('rid',11)->find();
            $this->assign('bz',$admin['bz']);
            $this->assign('data',$new_ary);
            return $this->fetch();
        }
    }
    public function changefen(){
        $id = input('id');
        $fen = input("fen");
        $data['fen'] = $fen;
        $data['sid'] = $id;
        $data['admin_id'] = $this->admin['id'];
        $data['time'] = time();
        $data['status'] = 0;
        $info = Db::name('dafen')->where('sid',$id)->where('admin_id',$this->admin['id'])->find();
        if($info){
            $res = Db::name("dafen")->where('id',$info['id'])->update(['fen'=>$fen]);
        }else{
        
            $res = Db::name("dafen")->insert($data);
            
        }
        if($res){
            ajaxMsg(1,'保存成功');
        }
        ajaxMsg(0,'网络错误');
    }
    public function dafenstatus(){
        $id = input('id');
        $res = Db::name("dafen")->where('sid',$id)->where('admin_id',$this->admin['id'])->update(['status'=>1]);
        // echo 123;die;
        //查看打分人
        if($this->admin['school']){
            //记录平均分
            $fen = Db::name("dafen as df")
                    ->join("admin ad",'ad.id=df.admin_id')    
                    ->where('sid',$id)
                    ->where("ad.school",$this->admin['school'])
                    ->where('df.status',1)
                    ->avg('fen');
            Db::name('shenqing')->where('id',$id)->update(['s_fen'=>$fen]);
        }elseif($this->admin['area']){
      
            //记录平均分
            $fen = Db::name("dafen as df")
                    ->join("admin ad",'ad.id=df.admin_id')    
                    ->where('df.sid',$id)
                    ->where("ad.area",$this->admin['area'])
                    ->where('df.status',1)
                    ->avg('fen');
                    
                    // echo  Db::name("dafen as df")->getlastsql();die;
            // echo $fen;die;
            Db::name('shenqing')->where('id',$id)->update(['a_fen'=>$fen]);
        }else{
            //记录平均分
            $fen = Db::name("dafen as df")
                    ->join("admin ad",'ad.id=df.admin_id')    
                    ->where('sid',$id)
                    ->where("ad.cid",0)
                    ->where('df.status',1)
                    ->avg('fen');
            Db::name('shenqing')->where('id',$id)->update(['c_fen'=>$fen]);
        }
        
        if($res){
            ajaxMsg(1,'评分成功');
        }
        ajaxMsg(1,'评分成功');
    }
}














